﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Stone.Library.Common
{
    public class LogManger
    {
        private static string logPath = string.Empty;
        private static EventLog logWin;

        static LogManger()
        {
            logWin = new EventLog();
            if (!System.Diagnostics.EventLog.SourceExists("SkyWalkHost"))
            {
                System.Diagnostics.EventLog.CreateEventSource(
                    "SkyWalkHost", "SkyWalkLog");
            }
            logWin.Source = "SkyWalkHost";
            logWin.Log = "SkyWalkLog";
        }
        /// <summary>
        /// 保存日志的文件夹
        /// </summary>
        public static string LogPath
        {
            get
            {
                if (logPath == string.Empty)
                {
                    logPath = AppDomain.CurrentDomain.BaseDirectory + @"Log\";
                    Console.WriteLine(logPath);
                }
                return logPath;
            }
            set { logPath = value; }
        }

        private static string logFielPrefix = string.Empty;

        /// <summary>
        /// 日志文件前缀
        /// </summary>
        public static string LogFielPrefix
        {
            get { return logFielPrefix; }
            set { logFielPrefix = value; }
        }

        /// <summary>
        ///  在文件中写日志
        /// </summary>
        /// <param name="logFile">日志文件名称</param>
        /// <param name="msg">日志信息</param>
        public static void WriteLogDoc(string logFile, string msg)
        {
            try
            {
                System.IO.StreamWriter sw = System.IO.File.AppendText(
                    LogPath + LogFielPrefix + logFile + " " +
                    DateTime.Now.ToLocalTime().ToString("yyyyMMdd") + ".Log"
                    );
                sw.WriteLine(DateTime.Now.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss: ") + msg);
                sw.Close();
            }
            catch
            { }
        }

        /// <summary>
        /// 向Windows写入成功日志
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="event_type">级别</param>
        public static void WriteSuccessLogWin(string msg)
        {
            logWin.WriteEntry(msg, EventLogEntryType.Information);
        }

        /// <summary>
        /// 向Windows写入错误日志
        /// </summary>
        /// <param name="msg"></param>
        public static void WriteErrorLogWin(string msg)
        {
            logWin.WriteEntry(msg, EventLogEntryType.Error);
        }

        /// <summary>
        /// 在windows的事件查看器中写入日志
        /// </summary>
        /// <param name="msg">日志内容</param>
        /// <param name="event_type">事件类别</param>
        /// <param name="event_src">事件来源</param>
        /// <param name="event_name">事件名称</param>
        public static void WriteLogWin(string msg,
            int event_type, string event_src, string event_name)
        {
            try
            {
                switch (event_type)
                {
                    case 0:
                        logWin.WriteEntry(msg, EventLogEntryType.Information);
                        break;
                    case 1:
                        logWin.WriteEntry(msg, EventLogEntryType.Warning);
                        break;
                    case 2:
                        logWin.WriteEntry(msg, EventLogEntryType.SuccessAudit);
                        break;
                    case 3:
                        logWin.WriteEntry(msg, EventLogEntryType.FailureAudit);
                        break;
                    case 4:
                        logWin.WriteEntry(msg, EventLogEntryType.Error);
                        break;
                }
            }
            catch (System.IO.FileNotFoundException exception)
            {
                logWin.WriteEntry(exception.Message, EventLogEntryType.Error);
            }
        }

        /// <summary>
        /// 日志类型
        /// </summary>
        public enum LogFile
        {
            Trace,
            Warning,
            Error,
            SQL
        }
    }
}
